|
|
|
|
|
|
|
Solution 2
The Last Error |
|
|
|
|
|
|
|
|
The FindWindow function returns either a window handle or zero. Zero is returned if no window is found or if an error occurred. How can you tell if an error occurred? That's where the LastError value comes in. Most API functions set an internal constant that indicates the meaning of an error, the value of which is only valid for functions that use the LastError capability. You can tell if a function uses the LastError value by reading the documentation for the function, which will usually say something like |
|
|
|
 |
|
 |
|
|
To get extended error information, call GetLastError. |
|
|
|
|
|
|
|
|
You can't actually call the GetLastError API function from Visual Basic, because Visual Basic clears the internal LastError value after each API call. Fortunately, Visual Basic allows you to retrieve the LastError value for the previous API call by accessing the LastDllError property of the Err object. |
|
|
|
|
|
|
|
|
How can you find the meaning of the LastError values? |
|
|
|
|
|
|
|
|
One way is to look in the api32.txt file or the winerror.h header file (on the CD that comes with this book) for a constant that corresponds to the value. This is not as haphazard as you might think. About halfway through the file, you'll find a long list of constants that begin with the prefix ERROR_. The list starts like this: |
|
|
|
|
|
|
|
|
' The configuration registry database operation completed
' successfully.
Public Const ERROR_SUCCESS = 0&
' Incorrect function.
Public Const ERROR_INVALID_FUNCTION = 1 ' dderror
' The system cannot find the file specified.
Public Const ERROR_FILE_NOT_FOUND = 2&
' The system cannot find the path specified.
Public Const ERROR_PATH_NOT_FOUND = 3& |
|
|
|
|
|
|
|
|
' The filename, directory name, or volume label syntax is incorrect.
Public Const ERROR_INVALID_NAME = 123& |
|
|
|
|
|